#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include<string.h>
#include "support.h"
#include "menubar.h"
#include "newinfo.h"
#include "addgroup.h"
#include "clist.h"
#include "file.h"
#include "exit.h"
#include "mainwindow.h"
#include "update.h"
#include "find.h"
#include "help.h"
#include  "about.h"

/*
 * callback for menubar item '新建信息' to handle activate singal
 */
void on_item_file_new_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	handle_for_create_new_info(user_data);
}

/*
 * callback for menubar item '添加分组' to handle activate singal
 */
void on_item_file_add_group_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	handle_for_create_window_add(user_data);
}


void on_item_file_save_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	struct file_ctrl *global_file=(struct file_ctrl *)user_data;
	if(global_file->data_mod) {
		save_all_file(global_file);
		global_file->data_mod=FALSE;
	}
}

void on_quit_easyblog_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	struct file_ctrl *global_file=(struct file_ctrl *)user_data;
	GtkWidget *dialog;
	gint sure;

	if(global_file->data_mod) {
		gchar msg[] = "数据已经被修改，是否保存?";
		dialog = gtk_message_dialog_new(GTK_WINDOW(window_main), 0,
				GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, msg);
		sure = gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
		if (sure == GTK_RESPONSE_OK) {
			save_all_file(global_file);
		}
	}
	exit_easyblog(global_file);
	gtk_main_quit();
}

void on_item_edit_find_info_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	handle_for_create_window_find(user_data);
}
                                        
void on_item_edit_open_link_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	#ifdef _DEBUG_
		g_print("on_item_edit_open_link_activate :------\n");
	#endif
	
	if(clist->show_all_row>0 && clist->show_selected_row != -1) {

		gchar *link;
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),
				clist->show_selected_row,4,&link);
		if(strlen(link)==0||strcmp(link,"---")==0 ){
			gchar msg[]="选中的信息博客记录为空!";
			message_box_for_toolbar(window_main,msg);
			return;
		}
		gchar blog[100];
		g_snprintf(blog,100,"firefox %s & >/dev/null",link);
		system(blog);
	}
}

void on_item_edit_delete_info_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	/*
	 * delete_info_from_clist_show:defined in clist.c.
	 * @user_data:       struct newinfo_struct
	 */
	delete_info_from_clist_show(user_data);
}


void on_item_edit_delete_group_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	delete_info_from_clist_group(user_data);
}


void on_item_edit_copy_all_activate (GtkMenuItem *menuitem,gpointer user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	GtkClipboard* clipboard;
	gchar *info[MAX_COLUMNS];
	gchar text[80];
	
	if(clist->show_selected_row != -1) {
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,0,&info[0]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,1,&info[1]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,2,&info[2]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,3,&info[3]);
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,4,&info[4]);
		g_snprintf(text,80,"%s %s %s %s %s",info[0],info[1],info[2],info[3],info[4]);
		clipboard=gtk_widget_get_clipboard(window_main, GDK_SELECTION_CLIPBOARD);
		gtk_clipboard_set_text(clipboard, text, strlen(text));
	} else {
		gchar msg[]="当前分组没有信息!";
		message_box_for_toolbar(window_main,msg);
	}	
}                                        
void
on_item_edit_copy_link_activate (GtkMenuItem *menuitem,gpointer user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	GtkClipboard *clipboard;
	gchar *text;
	
	if(clist->show_selected_row != -1) {
		gtk_clist_get_text(GTK_CLIST(clist->clist_show),clist->show_selected_row,4,&text);
		if(strcmp(text,"---")==0) {
			gchar msg[]="选中的信息博客记录为空!";
			message_box_for_toolbar(window_main,msg);
			return;
		}
		clipboard=gtk_widget_get_clipboard(window_main, GDK_SELECTION_CLIPBOARD);
		gtk_clipboard_set_text(clipboard, text, strlen(text));
	} else {
		gchar msg[]="选中的信息博客记录为空!";
		message_box_for_toolbar(window_main,msg);
	}	
}                                        


void
on_item_edit_update_group_activate     (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	struct addgroup_struct *addgroup=(struct addgroup_struct *)user_data;
	addgroup->window=(GtkWidget *)create_window_update_group(addgroup,addgroup->clist);
	gtk_widget_show(addgroup->window);
}


void
on_item_edit_update_info_activate      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	 newinfo->window=create_window_update_info (newinfo,newinfo->clist);
	 gtk_widget_show(newinfo->window);
}

void on_item_help_main_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	GtkWidget *textview;
	GtkTextBuffer *buffer;
	gint i,len;
	gchar tmp[80],*p=NULL;
	FILE *fp_help;
	textview=(GtkWidget *)create_window_help();
	buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));	
	p=find_pixmap_file("helpmain");
	if(p== NULL) {
		gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer),
						"没有帮助文件",strlen("没有帮助文件"));
	} else {
		if((fp_help=fopen(p,"r"))==NULL) {
			perror("fopen");
			return;
		}
		while(!feof(fp_help)) {
			fgets(tmp,80,fp_help);
			len=strlen(tmp);
			gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer),tmp,len);
			memset(tmp,'\0',len);
		}
		fclose(fp_help);
	}
}


void on_item_help_me_activate(GtkMenuItem *menuitem,gpointer user_data)
{
	GtkWidget *window;
	window=(GtkWidget *)create_window_about();

	gtk_widget_show(window);
	
}
